package state;

import java.util.Vector;

import particle.core.tools.ByteBuffer;
import particle.core.tools.File;
import state.socket.DataPackage;
import state.util.array.ByteArray;

public class RecordStore  {
  private DataPackage dataPackage = null;
  private String name = null;
  int size = 0;
  
  private RecordStore() {
  }
  
  public static RecordStore openRecordStore(String name) {
    String fullname = name + ".rms";
    File file1 = File.openFile(fullname, File.ModeRead);
    if (file1 == null) {
      file1 = File.openFile(fullname, File.ModeWrite);
      file1.close();
      file1 = File.openFile(fullname, File.ModeRead);
    }
    
    RecordStore recordStore = new RecordStore();
    ByteBuffer byteBuffer = new ByteBuffer();
    file1.read(byteBuffer);
    recordStore.name = fullname;
    if (byteBuffer.getBuffer() != null) {
      recordStore.dataPackage = DataPackage.createDataPackage(new ByteArray(byteBuffer.getBuffer(), byteBuffer.getLength()));
    } else {
      recordStore.dataPackage = DataPackage.createDataPackage1(0);
    }
    
    recordStore.dataPackage.resetReadHeader();
    while (recordStore.dataPackage.hasMoreData1(4)) {
      recordStore.dataPackage.nextByteArray();
      recordStore.size++;
    }
    recordStore.dataPackage.resetReadHeader();
    
    file1.close();
    return recordStore;
  }
  
  public static void deleteRecordStore(String name) {
    String fullname = name + ".rms";
    File.deleteFile(fullname);
  }

  public int getNumRecords() {
    return size;
  }

  public ByteArray getRecord(int recordNumber) {
    recordNumber = recordNumber - 1;
    if (size - 1 < recordNumber) {
     return null; 
    }
    
    int index = 0;
    dataPackage.resetReadHeader();
    while (dataPackage.hasMoreData1(4)) {
      ByteArray data = dataPackage.nextByteArray();
      if (index == recordNumber) {
        return data;
      }
      index++;
    }
    dataPackage.resetReadHeader();
    return null;
  }

  public int addRecord(ByteArray data, int offset, int length) {
    if (offset != 0 || length != data.len()) {
      ByteArray tmp = ByteArray.create(length);
      ByteArray.arraycopy(data, offset, tmp, 0, length);
      data = tmp;
    }
    
    dataPackage.putByteArray(data);
    
    ByteArray allData = dataPackage.getAllData();
    ByteBuffer byteBuffer = new ByteBuffer();
    byteBuffer.appendBytes(allData.array, allData.len());
    File file = File.openFile(name, File.ModeWrite);
    file.write(byteBuffer);
    file.close();
    size++;
    return size;
  }

  public Vector enumerateRecords() {
    Vector vector = new Vector();
    dataPackage.resetReadHeader();
    while (dataPackage.hasMoreData1(4)) {
      vector.add(dataPackage.nextByteArray());
    }
    dataPackage.resetReadHeader();
    return vector;
  }

  public static String[] listRecordStores() {
    // TODO: implement listRecordStores
    return null;
  }
}
